/**
 * Copyright (c) 2009, http://www.softwareinlife.com. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, 
 * with or without modification, are permitted provided that 
 * the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the http://www.softwareinlife.com nor the names of its 
 *    contributors may be used to endorse or promote products derived from 
 *    this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE SOFTWAREINLIFE AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * @author Kang, tae-ho (usteam@softwareinlife.com)
 * @date 2009. 10. 11.
 */

package com.softwareinlife.vision.android.ux.start;

import java.util.List;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.Toast;


import com.softwareinlife.vision.android.R;
import com.softwareinlife.vision.android.data.dao.BaseDAO;
import com.softwareinlife.vision.android.data.dao.UserDAO;
import com.softwareinlife.vision.android.data.exceptions.DatabaseConnectionException;
import com.softwareinlife.vision.android.data.exceptions.UserReadException;
import com.softwareinlife.vision.android.data.model.UserModel;
import com.softwareinlife.vision.android.ux.init.WelcomeActivity;
import com.softwareinlife.vision.android.ux.util.SharedPreferenceUtility;

public class VisionSoftwareInLifeActivity extends Activity implements
		OnTouchListener {
	/** Called when the activity is first created. */
	////////////////////////////////////////////////////////////////
	/////////////////////// Vision toast////////////////////////////
	///////////////////////////////////////////////////////////////
	/**
	 * toast context(space)
	 */
	private Context VisionContext;

	/**
	 * vision toast
	 */
	private Toast VisionToast;

	/**
	 * toast head
	 */
	private String VisionSoftString;

	/**
	 * indicate toast plan
	 */
	private String VisionMerongPlan;

	/**
	 * indicate toast time
	 */
	private String VisionMerongTime;

	/**
	 * indicate toast duration
	 */
	private int VisionToastDuration;

	/**
	 * indicate toast layout
	 */
	private LinearLayout VisionToastLinearlayout;

	private int VisionToastLinearlayoutHeight;

	private int VisionToastLinearlayoutWeight;

	/**
	 * indicate toast text3/1/2
	 */
	private TextView VisionToastTextView1;

	private TextView VisionToastTextView2;

	private TextView VisionToastTextView3;
	
	//////////////////////////////////////////////////////////////////////
	////////////////Notification Members//////////////////////////////////
	//////////////////////////////////////////////////////////////////////
	/**
	 * Notification
	 */
	private Notification notification = null;
	/**
	 * Notification id 
	 */
	//private static final int HELLO_ID = 1;
	
	private static final int CUSTOM_VIEW_ID= 1;
	
	/**
	 * Notification String
	 */
	private String visionNotification = null;
	
	/**
	 * Notification Manager
	 */
	private NotificationManager mNotificationManager = null;
	
	/**
	 * Notification icon
	 */
	private int icon;
	
	/**
	 * Notification CharactreSquence
	 */
	private CharSequence noteText = null;
	
	private CharSequence contentTitle = null;
	
	private CharSequence contentText = null;
	
	/**
	 * Notification show time setup
	 */
	private long when;
	
	/**
	 * Notification Context
	 */
	private Context context = null;
	
	/**
	 * On Create
	 */
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);

		// Initialize Database
		initDatabase();

		setContentView(R.layout.start_vision);

		// show toast
		showtoast();

		// Notification initialize & action
		showNotification();
	}
	
	/**
	 * Notification Initialize
	 */
	public void showNotification(){
		
		//notification is two case = notifiy bar + notify remote view 
		
		//notificateion string
		visionNotification = Context.NOTIFICATION_SERVICE;
		
		//drawable icon
		icon = R.drawable.icon;
		
		//show note
		noteText = "Update New Schedule!! Drag Icon!";
		//contentTitle = "Current Schedule";
		//contentText = "Schedule: read book || time : PM 3 : 30";
		
		//show note time
		when = System.currentTimeMillis();
		
		//notification manager
		mNotificationManager = (NotificationManager) getSystemService(visionNotification);
		
		//notification
		notification = new Notification(icon, noteText, when);
		
		//context
		context = getApplicationContext();
		
		//Notification Intent
		Intent notificationIntent = new Intent(this, VisionSoftwareInLifeActivity.class);
		
		//Notification PendingIntent
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
		
		//Notification Event
		notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    	
		//Notification Remote view & remote view settings 
		RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
        contentView.setImageViewResource(R.id.image, R.drawable.icon);
        contentView.setTextViewText(R.id.text, "[Schedule] : Read Book(Maxim) " +
        										"[Schedule_Time] : PM 15 : 30  " +
        										"[Click] : VisionSoftware Menu");
        notification.contentView = contentView;
        
        //Remote view click ! -> Activity -> VisionSoftwareMenu.class
        Intent notificationIntent1 = new Intent(this, VisionSoftwareMenuActivity.class);
        PendingIntent contentIntent1 = PendingIntent.getActivity(this, 0, notificationIntent1, 0);
        notification.contentIntent = contentIntent1;
        
        mNotificationManager.notify(CUSTOM_VIEW_ID, notification);
	
	}

	/**
	 * Vison Toast
	 * 
	 */
	private void showtoast() {

		VisionContext = getApplicationContext();

		// toast diplay
		VisionSoftString = "<Vision software in Life>";

		VisionMerongPlan = "Current Plan : Read Book!";

		VisionMerongTime = "Current Schedule : PM 03 : 30";

		// text view
		VisionToastTextView1 = new TextView(VisionContext);

		VisionToastTextView2 = new TextView(VisionContext);

		VisionToastTextView3 = new TextView(VisionContext);

		VisionToastTextView3.setText(VisionSoftString);

		VisionToastTextView1.setText(VisionMerongPlan);

		VisionToastTextView2.setText(VisionMerongTime);

		VisionToastTextView3.setTextSize(20);

		// VisionToastTextView3.setTextColor(R.color.button_color3);

		VisionToastTextView3.setBackgroundResource(R.color.string_color);

		// toast time duration
		VisionToastDuration = Toast.LENGTH_SHORT;

		// layout option
		VisionToastLinearlayout = new LinearLayout(VisionContext);

		VisionToastLinearlayout.setOrientation(LinearLayout.VERTICAL);

		// layout height/weight option
		VisionToastLinearlayoutHeight = LinearLayout.LayoutParams.FILL_PARENT;

		VisionToastLinearlayoutWeight = LinearLayout.LayoutParams.WRAP_CONTENT;

		VisionToastLinearlayout.setBackgroundColor(R.color.button_color3);

		// layout add text view 3/1/2
		VisionToastLinearlayout.addView(VisionToastTextView3,
				new LinearLayout.LayoutParams(VisionToastLinearlayoutHeight,
						VisionToastLinearlayoutWeight));

		VisionToastLinearlayout.addView(VisionToastTextView1,
				new LinearLayout.LayoutParams(VisionToastLinearlayoutHeight,
						VisionToastLinearlayoutWeight));

		VisionToastLinearlayout.addView(VisionToastTextView2,
				new LinearLayout.LayoutParams(VisionToastLinearlayoutHeight,
						VisionToastLinearlayoutWeight));

		VisionToastLinearlayout.setPadding(7, 50, 0, 50);

		// toast default setting
		VisionToast = Toast.makeText(VisionContext, VisionSoftString,
				VisionToastDuration);

		// toast set position
		VisionToast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);

		// toast view layout + text view
		VisionToast.setView(VisionToastLinearlayout);

		// run toast
		VisionToast.show();

	}

	/**
	 * Initialize Database
	 */
	private void initDatabase() {
		BaseDAO dao = null;

		try {
			dao = new BaseDAO(getApplicationContext());
			dao.createDatabase();
			dao.createTables();
		} catch (DatabaseConnectionException e) {
			e.printStackTrace();
		}

		dao.close();
	}

	public boolean onTouchEvent(MotionEvent event) {
		// Show Vision Menu
		showVisionMenu();

		return true;
	}

	public boolean onTouch(View v, MotionEvent event) {
		// Show Vision Menu
		showVisionMenu();

		return true;
	}

	public void showVisionMenu() {

		// Intent
		Intent intent = null;

		// UserModel
		List<UserModel> users = null;

		UserDAO dao = null;
		try {
			dao = new UserDAO(getApplicationContext());
			users = dao.readAll();

			// Check User 
			if (users != null && users.size() > 0) {

				// Shared Preference 
				SharedPreferenceUtility.setUserId(getApplicationContext(), users.get(0).getUserId());
				
				intent = new Intent(this, VisionSoftwareMenuActivity.class);
				startActivity(intent);
			} else {
				intent = new Intent(this, WelcomeActivity.class);
				startActivity(intent);
			}
		} catch (UserReadException e) {
			e.printStackTrace();
		} catch (DatabaseConnectionException e) {
			e.printStackTrace();
		}

		dao.close();
	}
}